<?php
/**
 * Created by PhpStorm.
 * User: 96151
 * Date: 2018/10/24
 * Time: 14:57
 */
namespace app\common\service;
use app\common\library\CommonFun;
use DictionaryAlertMsg;
use IGeTui;
use IGtAPNPayload;
use IGtAppMessage;
use IGtListMessage;
use IGtNotify;
use IGtTarget;
use IGtTransmissionTemplate;
use NotifyInfo_Type;

require_once __DIR__.'/getuiPush/IGt.Push.php';

class GeTuiService
{
    /**
     * @var IGeTui
     */
    public $IGeTui;

    public $GeTuiConfig;

    /**
     * 通知栏显示通知
     */
    const NOTIFICATION_BAR = "notification_bar";

    const MESSAGE_COUNT = "message_count";

    public function __construct()
    {
        CommonFun::get_other_config("getui",2);
        $GeTuiConfig = $this->GeTuiConfig = config("GeTui");
        $this->IGeTui = new IGeTui(null, $GeTuiConfig['APPKEY'], $GeTuiConfig['MASTERSECRET']);
    }

    //多推接口案例
    public function pushMessageToList( $cid_list ,$title, $body, $jump, $expire_time = 3600 * 1000 )
    {
        if ( !$cid_list ){
            return false;
        }

        $cid_list = array_unique( $cid_list );

        putenv("gexin_pushList_needDetails=true");
        putenv("gexin_pushList_needAsync=true");

        $igt = $this->IGeTui;
        $template = $this->IGtTransmissionTemplateDemo( $title, $body, $jump  );
        //个推信息体
        $message = new IGtListMessage();
        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime( $expire_time );//离线时间
        $message->set_data( $template );//设置推送消息类型
        $contentId = $igt->getContentId( $message );	//根据TaskId设置组名，支持下划线，中文，英文，数字


        $all_data_chunk = array_chunk( $cid_list, 995 );
        foreach ( $all_data_chunk as $value ){
            $targetList = array();
            foreach ( $value as $v ){
                //接收方
                $target1 = new IGtTarget();
                $target1->set_appId( $this->GeTuiConfig['APPID'] );
                $target1->set_clientId( $v );
                $targetList[] = $target1;
            }

            $igt->pushMessageToList($contentId, $targetList);
        }

        return $contentId;
    }

    public function pushMessageToApp( $title, $body, $jump , $expire_time = 3600 * 1000){
        $igt = $this->IGeTui;
        //定义透传模板，设置透传内容，和收到消息是否立即启动启用
        $template = $this->IGtTransmissionTemplateDemo( $title, $body, $jump );
        //$template = IGtLinkTemplateDemo();

        $message = new IGtAppMessage();
        $message->set_isOffline(true);
        $message->set_offlineExpireTime( $expire_time );//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
        $message->set_data($template);

        $appIdList=array( $this->GeTuiConfig['APPID'] );

        $message->set_appIdList($appIdList);

        $rep = $igt->pushMessageToApp($message);
        $rep = $rep['contentId'] ?: '';
        return $rep;
    }

    /**
     * 发送透传消息
     * @param $cid_list
     * @param $data
     * @param int $expire_time
     * @return bool
     */
    public function transmissionContentToList( $cid_list, $data, $expire_time = 3600 * 1000 )
    {
        if ( !$cid_list ){
            return false;
        }

        $cid_list = array_unique( $cid_list );

        putenv("gexin_pushList_needDetails=true");
        putenv("gexin_pushList_needAsync=true");

        $igt = $this->IGeTui;
        $template = $this->transmissionContent( $data  );
        //个推信息体
        $message = new IGtListMessage();
        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime( $expire_time );//离线时间
        $message->set_data( $template );//设置推送消息类型
        $contentId = $igt->getContentId( $message );	//根据TaskId设置组名，支持下划线，中文，英文，数字

        $all_data_chunk = array_chunk( $cid_list, 995 );
        foreach ( $all_data_chunk as $value ){
            $targetList = array();
            foreach ( $value as $v ){
                //接收方
                $target1 = new IGtTarget();
                $target1->set_appId( $this->GeTuiConfig['APPID'] );
                $target1->set_clientId( $v );
                $targetList[] = $target1;
            }

            $igt->pushMessageToList($contentId, $targetList);
        }
        return $contentId;
    }

    /**
     * 透传消息
     * @param array $data
     * @return IGtTransmissionTemplate
     */
    public function transmissionContent( array $data )
    {
        $template =  new IGtTransmissionTemplate();
        $template->set_appId($this->GeTuiConfig['APPID']);//应用appid
        $template->set_appkey($this->GeTuiConfig['APPKEY']);//应用appkey
        $template->set_transmissionType(2);//透传消息类型
        $template->set_transmissionContent( json_encode( $data ) );//透传内容
        return $template;
    }

    /**
     * @param $title
     * @param $content
     * @param $jump
     * @return IGtTransmissionTemplate
     */
    public function IGtTransmissionTemplateDemo( $title, $content, $jump ){
        $template =  new IGtTransmissionTemplate();
        $template->set_appId($this->GeTuiConfig['APPID']);//应用appid
        $template->set_appkey($this->GeTuiConfig['APPKEY']);//应用appkey
        $template->set_transmissionType(2);//透传消息类型
        $json = array(
            'title' => $title,
            'content' => $content,
            'jump' => $jump,
            'type' => self::NOTIFICATION_BAR
        );

        $template->set_transmissionContent( json_encode( $json ) );//透传内容

        $notify = new IGtNotify();
        $notify -> set_title( $title );
        $notify -> set_content( $content );
        $jump_url = urlencode( $jump );
        $notify -> set_intent(
            "intent:#Intent;launchFlags=0x04000000;component={$this->GeTuiConfig['component']}/{$this->GeTuiConfig['activity']};B.NOTIFICATION_MESSAGE_IS_START_ACTIVITY_INTENT=true;S.NOTIFICATION_MESSAGE_JUMP={$jump_url};end"
        );

        $notify -> set_type(NotifyInfo_type::_intent);
        //$notify ‐> set_url("https://dev.getui.com/");     //$notify ‐> set_type(NotifyInfo_type::_url);
        $template -> set3rdNotifyInfo($notify);

        //APN高级推送
        $apn = new IGtAPNPayload();
        $alertmsg = new DictionaryAlertMsg();
        $alertmsg->body = $content;

//        IOS8.2 支持
        $alertmsg->title = $title;

        $apn->alertMsg = $alertmsg;
        $apn->autoBadge = "+1";
        $apn->sound="";
        $apn->add_customMsg( "jump", $jump );
        $apn->contentAvailable=0;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);

        return $template;
    }
}